package com.vitabuybuy.tv.ui.mine.talent.usercenter

import android.os.Bundle
import android.text.Html
import android.view.View
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ActivityAddWithdrawAccountBinding
import com.library.common.base.BaseActivity
import com.library.common.base.MessageModel
import com.library.common.ext.setOnclickNoRepeat
import com.library.common.toast.XToast
import com.vitabuybuy.tv.ui.WebViewActivity
import com.vitabuybuy.tv.ui.mine.talent.usercenter.viewmodel.TalentWalletViewModel
import kotlinx.android.synthetic.main.activity_add_withdraw_account.*

class TalentAddWithdrawAccountActivity :
    BaseActivity<TalentWalletViewModel, ActivityAddWithdrawAccountBinding>() {
    override val isUseBaseTitleBar: Boolean
        get() = true
    override val layoutId: Int
        get() = R.layout.activity_add_withdraw_account

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(getString(R.string.add_account))
        tvReadAndAgree.text =
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
                Html.fromHtml(
                    mContext.getString(R.string.read_and_agree),
                    Html.FROM_HTML_MODE_COMPACT
                )
            } else {
                Html.fromHtml(mContext.getString(R.string.read_and_agree))
            }
    }

    override fun eventHandling() {
        setOnclickNoRepeat(tvReadAndAgree, btnNextStep){v->
            when (v) {
                tvReadAndAgree -> {
                    WebViewActivity.openActivity(mContext, "", "", 16)
                }
                btnNextStep -> {
                    when {
                        etName.text.toString()
                            .isEmpty() -> XToast.normal(getString(R.string.name_tip_2))
                        etCardNumber.text.toString()
                            .isEmpty() -> XToast.normal(getString(R.string.card_number_tip))
                        etBankBranch.text.toString()
                            .isEmpty() -> XToast.normal(getString(R.string.bank_branch_tip))
                        etBank.text.toString().isEmpty() -> XToast.normal(getString(R.string.bank_tip))
                        !ivChoose.isChecked -> XToast.normal(getString(R.string.please_agree_agreement))
                        else -> {
                            val params = HashMap<String, Any>().apply {
                                this["bankName"] = etBank.text.toString()
                                this["holderName"] = etName.text.toString()
                                this["number"] = etCardNumber.text.toString()
                                this["subbranch"] = etBankBranch.text.toString()
                            }
                            viewModel.addNewBankCard(params)
                        }
                    }
                }
            }
        }
    }

    override fun handleEvent(msg: MessageModel) {
        when (msg.code) {
            3 -> {
                XToast.success(getString(R.string.add_success))
                finish()
            }
        }
    }

    companion object {
        fun openActivity(mContext: BaseActivity<*, *>) {
            mContext.startActivity(null, TalentAddWithdrawAccountActivity::class.java)
        }
    }
}
